package cn.jsj.onclock.interceptor;


import cn.jsj.onclock.pojo.UserEnt;
import cn.jsj.onclock.pojo.identity.UserIdentityEnum;

/**
 * @author 卢斌
 */
public class RequestThreadContext {

    public static final ThreadLocal<UserEnt> threadStorage = new ThreadLocal<>();


    /**
     * 当前线程容器保存用户信息
     */
    public static void setUserEnt(UserEnt userEnt) {
        threadStorage.set(userEnt);
    }

    /**
     * 线程容器中获取发出请求的用户的Id
     */
    public static Long requesterUserId() {
        UserEnt userEnt = threadStorage.get();
        if (userEnt == null) {
            return null;
        }
        return threadStorage.get().getId();
    }

    /**
     * 线程容器中获取发出请求的用户角色
     */
    public static UserIdentityEnum requesterIdentity() {
        UserEnt userEnt = threadStorage.get();
        if (userEnt == null) {
            return null;
        }
        return threadStorage.get().getUserIdentityEnum();
    }

    /**
     * 清理线程容器资源
     */
    public static void cleanLocalVar() {
        threadStorage.remove();
    }

}
